<?php

declare(strict_types=1);

namespace app\backend\controller\crm;

use app\common\controller\Basic;
use app\common\service\crm\ClueService;
use app\common\service\crm\CustomerService;

/**
 * 客户线索表 控制器
 * @author 吕玉冰
 * @date  2022-07-19
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class Clue extends Basic
{

    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new ClueService();
        $this->customerService = new CustomerService();
    }

    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            return json($this->service->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            $data['create_user_id'] = $this->user['id'];
            $data['create_user_name'] = $this->user['real_name'];
            $data['owner_user_id'] = $this->user['id'];
            $data['owner_user_name'] = $this->user['real_name'];
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = [];
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            $data['create_user_id'] = $this->user['id'];
            $data['create_user_name'] = $this->user['real_name'];
            $data['owner_user_id'] = $this->user['id'];
            $data['owner_user_name'] = $this->user['real_name'];
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = $this->service->getInfoById($id);
            $this->service->_out_form_filter($data);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 添加编辑-表单数据处理
     */
    protected function _form_filter(&$data)
    {
        $data = rq_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = input('id', '');
            if ($id === 0) {
                return _jerr(1002);
            }
            return json($this->service->_del($id));
        }
        return _jerr(1000);
    }

    public function customer()
    {
        if ($this->request->isPost()) {
            $id = input('id', '');
            if ($id === 0) {
                return _jerr(1002);
            }
            $info = $this->service->getInfoById($id);
            $data = [];
            $data["id"] = $id;
            $data["delete_time"] = time();
            $this->service->saveForm($data);
            $data = [];
            $data['parent_id'] = $this->user['id'];
            $data['name'] = $info->name;
            $data['source'] = $info->source;
            $data['level'] = $info->level;
            $data['follow'] = $info->follow;
            return json($this->customerService->saveForm($data));
        }
        return _jerr(1000);

    }


}
    